package br.com.vacine;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import br.com.vacine.Entity.UsuarioEntity;
import br.com.vacine.dao.DB;
import br.com.vacine.util.ValidarCamposUtil;

public class CadastroUsuarioActivity extends ActionBarActivity implements EditText.OnClickListener {

	UsuarioEntity user = new UsuarioEntity();
	private EditText nomeEd;
	private EditText dataNascEd;
	private EditText emailEd;
	private RadioButton sexoFch;
	private RadioButton sexoMch;
	private int erros = 0;
	private static final int DATE_DIALOG_ID = 0;
	private DB db;
	

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cadastrar_usuario);
        	setDb(getDb());

        	nomeEd = (EditText)findViewById(R.id.editText_Nome);
			dataNascEd = (EditText)findViewById(R.id.editText_Data);
			dataNascEd.setOnClickListener(this);
			emailEd = (EditText)findViewById(R.id.editText_Email);
			sexoFch = (RadioButton)findViewById(R.id.checkBox_F);
			sexoMch = (RadioButton)findViewById(R.id.checkBox_M);
			
			if(!ValidarCamposUtil.isVerificaUsuario()){
				
				List<UsuarioEntity> lista = new ArrayList<UsuarioEntity>();
				 
				lista = getDb().filtroUsuario(TelaUsuarioActivity.getId());
				user.setIdUsuario(TelaUsuarioActivity.getId());
				nomeEd.setText(lista.get(0).getNome());
				dataNascEd.setText(lista.get(0).getDataNascimento());
				emailEd.setText(lista.get(0).getEmail());
				
				if(lista.get(0).getSexo().equals("Masculino")){
					sexoMch.setChecked(true);
				}
				if(lista.get(0).getSexo().equals("Feminino")){
					sexoFch.setChecked(true);
				}
			}
	
	}
	
	public void bt_Cadastrar(View v) throws ParseException{
		
		user.setNome(nomeEd.getText().toString());
		user.setDataNascimento(dataNascEd.getText().toString());
		user.setEmail(emailEd.getText().toString());
		
			if(sexoFch.isChecked()){
				user.setSexo(sexoFch.getText().toString());
			}
			if(sexoMch.isChecked()){
				user.setSexo(sexoMch.getText().toString());
			}
		
		
		if (nomeEd.getText().toString().length() <= 3) {
			
				nomeEd.setError(getText(R.string.msgUsuarioNomeInvalido));
				erros++;
		
			if (nomeEd.getText().toString().length() <= 0){
				
				nomeEd.setError(getText(R.string.msgErroCampoObrigatorio));
				nomeEd.requestFocus();
			}
		} 
			
		
		if(ValidarCamposUtil.validaData(dataNascEd.getText().toString())){
			dataNascEd.setError(getText(R.string.msgFormatoInvalido));
			erros++;
		}
		
		if (dataNascEd.getText().toString().length() <= 0) {
			dataNascEd.setError(getText(R.string.msgErroCampoObrigatorio));
			dataNascEd.requestFocus();
			erros++;
		}else{
			if(!ValidarCamposUtil.isValidaData(dataNascEd.getText().toString())){
				dataNascEd.setError(getText(R.string.msgDataSuperiorAtual));
				Toast.makeText(this, R.string.msgDataSuperiorAtual, Toast.LENGTH_LONG).show();
				erros++;
				}
			}

		if (emailEd.getText().toString().length() <= 0) {
			emailEd.setError(getText(R.string.msgErroCampoObrigatorio));
			emailEd.requestFocus();
			erros++;
		}else 
			if(!ValidarCamposUtil.validaFormatoEmail(emailEd.getText().toString())) {
			emailEd.setError(getText(R.string.msgFormatoInvalido));
			erros++;
		}
		
		if(!sexoFch.isChecked() && !sexoMch.isChecked()){
			erros++;
		}
		
		if (erros == 0) {
			
			if(!ValidarCamposUtil.isVerificaUsuario()){
				getDb().update(user, 2);
				Toast.makeText(this, R.string.msgAtualizarDados, Toast.LENGTH_SHORT).show();
				finish();
			}else
				if(getDb().pesquisaUsuario(nomeEd.getText().toString())){
					nomeEd.setError(getText(R.string.msgErroUsuarioIdentico));
				}else{
					getDb().insert(user, 2);
					Toast.makeText(this, R.string.msgUsuarioCadastrado, Toast.LENGTH_LONG).show();
					finish();
				}
			
			
		}else{
			
			Toast.makeText(this, R.string.msgErroNaoCadastrado, Toast.LENGTH_SHORT).show();
			erros = 0;
		}
	}

	@Override
	public Dialog onCreateDialog(int id){
		
		Calendar calendario = Calendar.getInstance();
		
		int ano = calendario.get(Calendar.YEAR);
		int mes = calendario.get(Calendar.MONTH);
		int dia = calendario.get(Calendar.DAY_OF_MONTH);
		
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, ano, mes, dia);
			
		}
		
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			
			String data = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear+1) + "/" + String.valueOf(year);

			dataNascEd.setText(data);
		}
	};

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		
		if(v == dataNascEd){
			showDialog(DATE_DIALOG_ID);
		}
	}

	public DB getDb() {
		return db;
	}
	
	public void setDb(DB db) {
		this.db = new DB(this);
	}
	
}

